<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-12-5
 * Time: 下午9:26
 */
namespace app\wateradmin\controller;

use app\common\controller\BaseAdmin;
use think\Db;

class Admingoodsclass extends BaseAdmin{
    /*
     * 后台首页
     * 赵坤
     * 20161206
     * */
    public function index(){
        $this->assign('title','拼图云商品分类管理');
        $this->assign('description','拼图云商城');
        $goodsclass = cache('goodsclass'.session('shop'));
        if(empty($goodsclass)){
            $oneclass = Db::name('shop_goods_class')->where(array('gc_parent_id'=>0,'seller_id'=>session('shop')))->order('gc_sort DESC,gc_id')->select();
            foreach ($oneclass as $key=>$value){
                $twoclass = Db::name('shop_goods_class')->where(array('gc_parent_id'=>$value['gc_id'],'seller_id'=>session('shop')))->order('gc_sort DESC,gc_id')->select();
                if(!empty($twoclass)){
                    foreach($twoclass as $k=>$val){
                        $three = Db::name('shop_goods_class')->where(array('gc_parent_id'=>$val['gc_id'],'seller_id'=>session('shop')))->order('gc_sort DESC,gc_id')->select();
//                        if(empty($three)){
//                            $three=$val;
//                        }
                        $twoclass[$k]['child'] = $three;
                    }
                }
//                else{
//                    $twoclass[0] = $value;
//                    $twoclass[0]['child'][0]=$value;
//                }
                $oneclass[$key]['child']=$twoclass;
            }
            $goodsclass = $oneclass;
            cache('goodsclass'.session('shop'),$goodsclass);
        }
        $this->assign('goodsclass',$goodsclass);
        return $this->fetch();
    }
    /*
     * 分类添加
     * 赵坤
     * 20161221
     * */
    public function add(){
        $this->assign('title','拼图云商品分类添加');
        $this->assign('description','拼图云商城');
        if($this->request->param('id')){
            $id = $this->request->param('id');
            $this->assign('gc_parent_id',intval($id));
        }else{
            $this->assign('gc_parent_id',0);
        }
        if($this->request->isPost()){
            $gc = array(
                'gc_name'=>$this->request->param('gc_name'),
                'gc_icon'=>$this->request->param('goodsclass'),
                'seller_id'=>session('shop'),
                'gc_parent_id'=>$this->request->param('gc_parent_id'),
                'commis_rate'=>$this->request->param('commis_rate'),
                'gc_sort'=>intval($this->request->param('gc_sort')),
                'gc_banner'=>$this->request->param('classadv'),
                'gc_bannerlink'=>$this->request->param('gc_bannerlink'),
                'gc_description'=>$this->request->param('gc_description'),
                'is_show'=>$this->request->param('is_show')
            );
            $id = Db::name('shop_goods_class')->insertGetId($gc);
            if($id!==false){
                cache('goodsclass'.session('shop'),null);
                $this->redirect('admingoodsclass/index');
            }
        }
        return $this->fetch();
    }
    /*
     * 编辑
     * 赵坤
     * 20161221
     * */
    public function edit(){
        $this->assign('title','拼图云商品分类编辑');
        $this->assign('description','拼图云商城');
        if($this->request->param('id')){
            $id = $this->request->param('id');
            $gc_info = Db::name('shop_goods_class')->where(array('gc_id'=>$id,'seller_id'=>session('shop')))->find();
            $this->assign('gc_info',$gc_info);
        }else{
            $this->assign('gc_info','');
        }
        if($this->request->isPost()){
            $gc = array(
                'gc_name'=>$this->request->param('gc_name'),
                'gc_icon'=>$this->request->param('goodsclass'),
                'seller_id'=>session('shop'),
                'gc_parent_id'=>$this->request->param('gc_parent_id'),
                'commis_rate'=>$this->request->param('commis_rate'),
                'gc_sort'=>intval($this->request->param('gc_sort')),
                'gc_banner'=>$this->request->param('classadv'),
                'gc_bannerlink'=>$this->request->param('gc_bannerlink'),
                'gc_description'=>$this->request->param('gc_description'),
                'is_show'=>$this->request->param('is_show')
            );
            $id = Db::name('shop_goods_class')->where(array('gc_id'=>$id))->update($gc);
            if($id!==false){
                $this->redirect('admingoodsclass/index');
            }
        }
        return $this->fetch();
    }
    /*
     * 删除
     * 赵坤
     * 20161221
     * */
    public function del(){

    }

}